home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLSRC.PAK / VSLIDER.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  5.6 KB  |  225 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.6  $
  6. //
  7. // Implementation of TVSlider, vertical slider UI widget
  8. //----------------------------------------------------------------------------
  9. #pragma hdrignore SECTION
  10. #include <owl/pch.h>
  11. #if !defined(OWL_SLIDER_H)
  12. # include <owl/slider.h>
  13. #endif
  14. #if !defined(OWL_DC_H)
  15. # include <owl/dc.h>
  16. #endif
  17. #if !defined(OWL_COMMCTRL_H)
  18. # include <owl/commctrl.h>
  19. #endif
  20. #if !defined(OWL_UIHELPER_H)
  21. # include <owl/uihelper.h>
  22. #endif
  23.  
  24. OWL_DIAGINFO;
  25.  
  26. #if !defined(SECTION) || SECTION == 1
  27.  
  28. //
  29. // Constructor for a TVSlider object
  30. //
  31. TVSlider::TVSlider(TWindow*        parent,
  32.                    int             id,
  33.                    int x, int y, int w, int h,
  34.                    TResId          thumbResId,
  35.                    TModule*        module)
  36. :
  37.   TSlider(parent, id, x, y, w, h, thumbResId, module)
  38. {
  39.   if (!w)
  40.     Attr.W = 32;
  41.   Attr.Style |= TBS_VERT;  // In case it is Native, else we dont care
  42. }
  43.  
  44. //
  45. // Constructor for a slider object created from resource
  46. //
  47. TVSlider::TVSlider(TWindow*        parent,
  48.                    int             resId,
  49.                    TResId          thumbResId,
  50.                    TModule*        module)
  51. :
  52.   TSlider(parent, resId, thumbResId, module)
  53. {
  54. }
  55.  
  56. //----------------------------------------------------------------------------
  57. // Protected implementation
  58.  
  59. // Leave the whole implemenation out if built with OWL_NATIVECTRL_ALWAYS
  60. //
  61. #if !defined(OWL_NATIVECTRL_ALWAYS)
  62.  
  63. //
  64. // Calculate and return position given a thumb upper left point and vice versa.
  65. //
  66. int
  67. TVSlider::PointToPos(const TPoint& point)
  68. {
  69.   int pixelRange = Attr.H - ThumbRect.Height() - 2; // 2 is room for focus rect
  70.   long scaledY = long(point.y-1) * long(Range);
  71.   return int(scaledY/pixelRange + Min);
  72. }
  73.  
  74. //
  75. //
  76. //
  77. TPoint
  78. TVSlider::PosToPoint(int pos)
  79. {
  80.   int pixelRange = Attr.H - ThumbRect.Height() - 2; // 2 is room for focus rect
  81.   long relPos = pos - Min;
  82.   int left = (Attr.W-ThumbRect.Width()-2)/2+1;
  83.   return TPoint(left, int((relPos*pixelRange)/Range)+1);
  84. }
  85.  
  86. //
  87. // Notify parent of a scroll event by sending a WM_VSCROLL message
  88. //
  89. void
  90. TVSlider::NotifyParent(int scrollCode, int pos)
  91. {
  92. #if defined(BI_PLAT_WIN32)
  93.   Parent->HandleMessage(WM_VSCROLL, MkParam1(scrollCode, pos), TParam2(GetHandle()));
  94. #else
  95.   Parent->HandleMessage(WM_VSCROLL, scrollCode, MkParam2(pos, GetHandle()));
  96. #endif
  97. }
  98.  
  99. //
  100. // Determines if a point is within the thumb, or other hot areas of the
  101. // slider. Uses region if available, else uses thumb bounding rect.
  102. // Returns -1 if no hit.
  103. //
  104. int
  105. TVSlider::HitTest(TPoint& point)
  106. {
  107.   if (ThumbRgn ? ThumbRgn->Contains(point) : ThumbRect.Contains(point))
  108.     return SB_THUMBTRACK;
  109.  
  110.   if (point.x > ThumbRect.right || point.x < ThumbRect.left)
  111.     return SB_THUMBPOSITION;
  112.  
  113.   else if (point.y < ThumbRect.top)
  114.     return SB_PAGEUP;
  115.  
  116.   else if (point.y >= ThumbRect.bottom)
  117.     return SB_PAGEDOWN;
  118.  
  119.   return -1;
  120. }
  121.  
  122. //
  123. // Paint the ruler. The ruler doesn't overlap with the thumb or slot.
  124. // SysColors for text fg or bg are never dithered & can use TextRect.
  125. //
  126. void
  127. TVSlider::PaintRuler(TDC& dc)
  128. {
  129.   int    ticW = (Attr.W-ThumbRect.Width()-2)/2;
  130.  
  131.   // Clear ruler areas to bk color
  132.   //
  133.   dc.SetBkColor(BkColor);
  134.   dc.TextRect(0, 0, ticW+1, Attr.H);
  135.   dc.TextRect(Attr.W-ticW-1, 0, Attr.W, Attr.H);
  136.  
  137.   // Draw bottom tic & internal tics if any, then top tic
  138.   //
  139.   int    margin = ThumbRect.Height()/2;
  140.   int    y;
  141.  
  142.   dc.SetBkColor(TColor::SysBtnText);
  143.  
  144.   if (Tics && TicCount) {
  145.     for (int i = 0; i < TicCount; i++) {
  146.       y = PosToPoint(Tics[i]).y + margin;
  147.       dc.TextRect(0, y, ticW, y+1);
  148.       dc.TextRect(Attr.W-ticW-1, y, Attr.W-1, y+1);
  149.     }
  150.   }
  151.   else {
  152.     for (int i = Min; i < Max; i += TicGap) {
  153.       y = PosToPoint(i).y + margin;
  154.       dc.TextRect(0, y, ticW, y+1);
  155.       dc.TextRect(Attr.W-ticW-1, y, Attr.W-1, y+1);
  156.       if (!TicGap)
  157.         break;
  158.     }
  159.   }
  160.   y = Attr.H-margin-1;
  161.   dc.TextRect(0, y, ticW, y+1);
  162.   dc.TextRect(Attr.W-ticW-1, y, Attr.W-1, y+1);
  163. }
  164.  
  165. //
  166. // Paint the slot that the thumb slides over.
  167. //
  168. void
  169. TVSlider::PaintSlot(TDC& dc)
  170. {
  171.   int    ticW = (Attr.W-ThumbRect.Width()-2)/2;
  172.   int    vmargin = ThumbRect.Height()/2;             // top & bottom margins
  173.   int    hmargin = (ThumbRect.Width()-SlotThick)/2;  // left & right margins
  174.  
  175.   // Draw margins around slot in background color
  176.   //
  177.   dc.SetBkColor(BkColor);
  178.   // Above
  179.   dc.TextRect(ticW+1, 0, ticW+1+ThumbRect.Width(), vmargin);
  180.   // Left
  181.   dc.TextRect(ticW+1, vmargin, ticW+1+hmargin, Attr.H);
  182.   // Right
  183.   dc.TextRect(Attr.W-ticW-1-hmargin, vmargin, Attr.W-ticW-1, Attr.H);
  184.   // Bottom
  185.   dc.TextRect(ticW+1, Attr.H-vmargin, ticW+1+ThumbRect.Width(), Attr.H);
  186.  
  187.   // Draw slot frame, shadow, fill & highlight to the right
  188.   //
  189.   TRect slotR(ticW+1+hmargin, vmargin, ticW+1+hmargin+SlotThick, Attr.H-vmargin);
  190.   TUIBorder b(slotR, TUIBorder::WndRecessed);
  191.   b.Paint(dc);
  192. }
  193.  
  194. #endif  // !defined(OWL_NATIVECTRL_ALWAYS)
  195.  
  196. #endif
  197. #if !defined(SECTION) || SECTION == 2
  198.  
  199. IMPLEMENT_STREAMABLE1(TVSlider, TSlider);
  200.  
  201. #if !defined(BI_NO_OBJ_STREAMING)
  202.  
  203. //
  204. //
  205. //
  206. void*
  207. TVSlider::Streamer::Read(ipstream& is, uint32 /*version*/) const
  208. {
  209.   ReadBaseObject((TSlider*)GetObject(), is);
  210.   return GetObject();
  211. }
  212.  
  213. //
  214. //
  215. //
  216. void
  217. TVSlider::Streamer::Write(opstream& os) const
  218. {
  219.   WriteBaseObject((TSlider*)GetObject(), os);
  220. }
  221.  
  222. #endif  // if !defined(BI_NO_OBJ_STREAMING)
  223.  
  224. #endif
  225.